package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.NewsDaoImpl;
import com.ysd.dao.impl.UserDaoImpl;
import com.ysd.entity.User;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


/**
 * localhost:8080/kouhong/admincomment?reqName=getComm
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
public class AdminCommServlet extends BaseServlet {
	
	public void getComm(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDaoImpl usDao=new UserDaoImpl();
		String status = request.getParameter("status");
		int page =0;
		if(request.getParameter("page")!=null && !request.getParameter("page").equals("")) {
			 page =Integer.parseInt(request.getParameter("page"));
		}
		int size = 0;
		if(request.getParameter("size")!=null && !request.getParameter("size").equals("")) {
			 size = Integer.parseInt(request.getParameter("size"));
		}
		
		List<User> usList=usDao.getUserStatus(status, page, size);
		
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式为UTF-8
		PrintWriter out = response.getWriter();//回信者

		Gson gson = new Gson();
		String res = gson.toJson(usList);
		out.print(res);
		out.flush();
		out.close();
		
	}

/**
 * localhost:8080/kouhong/admincomment?reqName=getCommById
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
	public void getCommById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDaoImpl usDao=new UserDaoImpl();
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式为UTF-8
		PrintWriter out = response.getWriter();//回信者
		int uId = 0;
		if(request.getParameter("userId")!=null && !request.getParameter("userId").equals("")) {
			 uId = Integer.parseInt(request.getParameter("userId"));	
		}
		User user=usDao.getUserById(uId);
		

		Gson gson = new Gson();
		String res = gson.toJson(user);
		out.print(res);
		out.flush();
		out.close();
		
	}

/**
 * localhost:8080/kouhong/admincomment?reqName=updateCommById
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
	public void updateCommById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDaoImpl usDao=new UserDaoImpl();
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式为UTF-8
		PrintWriter out = response.getWriter();//回信者
		int userId = 0;
		if(request.getParameter("userId")!=null && !request.getParameter("userId").equals("")) {
			 userId = Integer.parseInt(request.getParameter("userId"));	
		}
		User user = new User(userId, "已读",null);
		int n = usDao.updateuserById(user);
		if (n>0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}

		
		out.flush();
		out.close();
		
	}
	

/**
 * localhost:8080/kouhong/admincomment?reqName=getCommReplyById
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
	public void getCommReplyById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDaoImpl usDao=new UserDaoImpl();
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式为UTF-8
		PrintWriter out = response.getWriter();//回信者
		int userId = 0;
		if(request.getParameter("userId")!=null && !request.getParameter("userId").equals("")) {
			 userId = Integer.parseInt(request.getParameter("userId"));	
		}
		String reply = request.getParameter("reply");
		User user = new User(userId, null, reply);
		int n = usDao.updateuserReplyById(user);
				
		if (n>0) {
			out.print(Result.toClient(true, "回复成功", ""));
		} else {
			out.print(Result.toClient(false, "回复失败", ""));
		}

		
		out.flush();
		out.close();
		
	}
	/**
	 * 总数
	 * localhost:8080/kouhong/admincomment?reqName=getCommPage
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	
	  public void getCommPage(HttpServletRequest request, HttpServletResponse
	  response) throws ServletException, IOException {
		  response.setCharacterEncoding("UTF-8");//设置响应的编码格式为UTF-8 
		  PrintWriter out = response.getWriter();//回信者
		  UserDaoImpl usDao=new UserDaoImpl();
	  String status = request.getParameter("status");
	  int n=usDao.getUser(status);
	  
	  out.print(n);
	 
	  out.flush();
	  out.close();
	  
	 }
	  
	 
	/**
	 * 删除产品分类
	 * localhost:8080/kouhong/admincomment?reqName=deluser
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deluser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//接受前台传来的参数
		int userId=Integer.parseInt(request.getParameter("userId"));
		UserDaoImpl	 Dao = new UserDaoImpl();
		
		response.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out = response.getWriter();//out对像
		
		int n =Dao.deluserId(userId);
		if (n>0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		
		out.flush();
		out.close();
	}
	

/**
 * 批量删除
 * localhost:8080/kouhong/admincomment?reqName=deleteMessageById
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
	public void deleteMessageById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDaoImpl usDao=new UserDaoImpl();
		response.setCharacterEncoding("UTF-8");//设置响应的编码格式为UTF-8
		PrintWriter out = response.getWriter();//回信者
		String ids = request.getParameter("ids");
		int n = usDao.deleteMessageByIds(ids);
		if (n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		out.flush();
		out.close();
		
	}


}
